package ueb15;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 * Interactive viewer for Red-Black-Tree Implementation. Viewer of the MVC.
 */
public class RedBlackTreeGui {

	private JButton addButton;
	private final Controller controller;
	private final JFrame frame = new JFrame("Red Black Tree Gui");
	private JButton restartButton;
	private JTextField textfield;

	public RedBlackTreeGui(Controller controller) {
		this.controller = controller;
	}

	public void showGui() {
		createTreeView();
	}

	private void createAddButton() {
		addButton = new JButton("Read File");
		addButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				final Object input = validateInput(textfield.getText());
                                String line     = null;
                                String fileName = null;
                                StringLineReader reader = null;
                                boolean eof = false;
                                int counter = 0;
				if (input != null) {
                                    if(input instanceof String)
                                        fileName = (String) input;
                                    try {
                                        reader = new StringLineReader(fileName);
                                    } catch(Exception ex) {
                                        System.err.println("Fehler!");
                                        ex.printStackTrace();
                                    }
                                    
                                    while(!eof) {
                                        try {
                                            line = reader.read(counter);
                                        } catch(IOException io) {
                                            eof = true;
                                            counter = 0;
                                        }
                                        
                                        if(line != null) {
                                            controller.addValue(line);
                                        }
                                        
                                        counter++;
                                    }
                                    
                                    counter = 0;
                                    //add((String) input);
				}
				textfield.setText("");
				textfield.requestFocus();
			}

		});
	}

	private JPanel createButtonPanel() {
		createRestartButton();
		createAddButton();

		JPanel allButtonPanel = new JPanel();
		allButtonPanel.setLayout(new GridLayout(1, 6));
		textfield = new JTextField();
		allButtonPanel.add(restartButton);
		allButtonPanel.add(textfield);
		allButtonPanel.add(addButton);

		return allButtonPanel;
	}

	private void createRestartButton() {
		restartButton = new JButton("Restart");
		restartButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.restart();
				textfield.setText("");
				textfield.requestFocus();
			}
		});
	}


	private void createTreeView() {
		Container cp = frame.getContentPane();
		cp.setLayout(new BorderLayout());
		JPanel rightPanel = createButtonPanel();
		cp.add(controller.getView(), BorderLayout.CENTER);
		cp.add(rightPanel, BorderLayout.PAGE_END);
		// If there is nothing to cleanup, we can call
		// frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				controller.stopGui();
			}
		});

		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.toFront();
		textfield.requestFocus();
	}

	private Object validateInput(String input) {
		if (input.isEmpty()) {
			JOptionPane.showMessageDialog(null, "Textfield is empty!", "Error", JOptionPane.OK_OPTION);
			return null;
		}
		try {
			return (input);
		} catch (NumberFormatException exc) {
			JOptionPane.showMessageDialog(null, "Bitte geben Sie eine Zahl ein!", "Error", JOptionPane.OK_OPTION);
			return null;
		}
	}
             
}
